<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\exception\ValidateException;

use service\JsonService as Json;
use app\admin\controller\BaseController;

use app\common\model\User;
use app\common\model\System;
use app\common\model\Notice;
use app\common\model\NoticeSign;
use app\common\model\UserResume;
use app\common\model\ResumeRound;
use app\common\model\SystemMenu;
use app\common\model\UserSection;
use app\common\model\UserSectionLevel;
use app\common\model\SystemRole;
use app\common\model\UserSalaryType;
use app\common\model\Job;
use app\common\model\Feedback;
use app\common\model\Message;
use app\common\model\CheckApprove;
use app\common\model\KpiRecord;
use app\common\model\UserSectionCate;
use app\common\model\RuleKpi;
use app\common\model\UserCompany;
use app\common\model\CheckImg;

use app\common\validate\NoticeValidate;
use app\common\validate\SectionValidate;
use app\Request;

class SystemController extends BaseController
{
    /**
     * 获取系统配置信息
     */
    public function getSystem()
    {
        return Json::success('ok', System::getFind(['id' => 1]));
    }

    /**
     * 系统配置编辑
     */
    public function setSystem()
    {
        $flag = system::edit(input(''));
        if(!$flag) return Json::fail('编辑失败');
        return Json::success('编辑成功');
    }

    /**
     * 首页数据
     */
    public function getHomeData()
    {
        /** 信息中心 */
        $backData['resume_num_all'] = UserResume::where([['is_staff', '<>', 1]])->count();

        /** 岗位需求 */
        $backData['need_num_all'] = Job::where(['is_show' => 1, 'status' => 1])->sum('need_num');

        /** 邀约 */
        $backData['invite_num_all'] = ResumeRound::where([['status', '>=', 2]])->count();

        /** 业绩目标 */
        $backData['achiev_num_all'] = KpiRecord::sum('total_target');

        return Json::success('ok', $backData);
    }

    /**
     * 首页折线图
     */
    public function getHomeMap()
    {
        $month = date('m');
        for($i = 1; $i <= $month; $i++) $monthArr[] = $i;

        foreach($monthArr as $month){
            $resume[]    = UserResume::where([['is_staff', '<>', 1]])->whereMonth('create_time', date('Y') . '-' . $month)->count();
            $nendNum[]   = Job::where(['is_show' => 1, 'status' => 1])->whereMonth('create_time', date('Y') . '-' . $month)->sum('need_num');
            $inviteNum[] = ResumeRound::where([['status', 'in', [2, 3, 4, 5]]])->whereMonth('invite_time', date('Y') . '-' . $month)->count();

            $kpiIdsFst   = KpiRecord::whereMonth('data_time', date('Y') . '-' . $month)->column('rule_kpi_id');
            $achievNum[] = RuleKpi::where([['id', 'in', $kpiIdsFst]])->sum('total_high');
        }

        foreach($monthArr as &$month) $month .= '月';

        $data['resume']     = ['day' => $monthArr, 'data' => $resume];
        $data['nend_num']   = ['day' => $monthArr, 'data' => $nendNum];
        $data['invite_num'] = ['day' => $monthArr, 'data' => $inviteNum];
        $data['achiev_num'] = ['day' => $monthArr, 'data' => $achievNum];

        return Json::success('ok', $data);
    }



    
    /**
     * 获取通知公告列表
     */
    public function getNoticeList()
    {
        $page       = Request()->param('page', 1);
        $search     = Request()->param('search', '');
        $startTime  = Request()->param('start_time', '');
        $endTime    = Request()->param('end_time', '');

        $where = [];

        if(!empty($search)) $where[] = ['title', 'like', '%' . $search . '%'];
        if(!empty($startTime) && empty($endTime)) $where[] = ['create_time', '>=', strtotime($startTime)];
        if(empty($startTime) && !empty($endTime)) $where[] = ['create_time', '<=', strtotime($endTime) + 86399];
        if(!empty($startTime) && !empty($endTime)) $where[] = ['create_time', 'between', [strtotime($startTime), strtotime($endTime) + 86399]];

        $field = 'id, user_id, title, content, send_type, receive_ids, create_time';
        $list = Notice::getList($where, 1, $page, 20, $field, 'create_time DESC');
        if($list === false) return Json::fail('暂无数据');

        $sendType     = [1 => '所有人', 2 => '部门', 3 => '个人'];
        foreach($list as &$info){
            switch($info['send_type']){
                case 2:
                    $receiveIds = explode(',', $info['receive_ids']);
                    $receiveArr = UserSection::getCol([['id', 'in', $receiveIds]], 'name');
                    $info['receive_name'] = implode(',', $receiveArr);
                    break;
                case 3:
                    $receiveIds = explode(',', $info['receive_ids']);
                    $receiveArr = User::getCol([['id', 'in', $receiveIds]], 'name');
                    $info['receive_name'] = implode(',', $receiveArr);
                    break;
                default:
                    $info['receive_name'] = '';
                    break;
            }

            $info['send_name'] = User::getVal(['id' => $info['user_id']], 'name');
            $info['send_type'] = $sendType[$info['send_type']];
            $info['sign_nums'] = NoticeSign::getNum(['notice_id' => $info['id']]);

            unset($info['user_id']);
            unset($info['send_type']);
            unset($info['receive_ids']);
        }

        return Json::success('ok', $list, Notice::getNum($where));
    }

    /**
     * 获取我的通知列表
     */
    public function getMyNoticeList()
    {
        $page      = Request()->param('page', 1);
        $sectionId = Request()->param('section_id', 0);
        $search    = Request()->param('search', '');
        $startTime = Request()->param('start_time', '');
        $endTime   = Request()->param('end_time', '');

        $where = '(FIND_IN_SET(' . $sectionId . ', receive_ids) or send_type=1)';

        if(!empty($search)) $where .= " and 'title' LIKE '%" . $search . "%'";
        if(!empty($startTime) && empty($endTime)) $where .= ' and create_time >=' . strtotime($startTime);
        if(empty($startTime) && !empty($endTime)) $where .= ' and create_time <=' . ((int)strtotime($endTime) + 86399);
        if(!empty($startTime) && !empty($endTime)) $where .= ' and create_time BETWEEN ' . strtotime($startTime) . ' and ' . ((int)strtotime($endTime) + 86399);

        $page = ($page - 1) * 20;

        $field = 'id, user_id, title, content, send_type, receive_ids, create_time';
        $list = Notice::where($where)
            ->field($field)
            ->limit($page, 20)
            ->order('create_time DESC')
            ->select();
            // ff(Notice::getLastSql());
        if($list->isEmpty()) return Json::fail('暂无数据');

        $sendType     = [1 => '所有人', 2 => '部门', 3 => '个人'];
        foreach($list as &$info){
            switch($info['send_type']){
                case 2:
                    $receiveIds = explode(',', $info['receive_ids']);
                    array_pop($receiveIds);
                    $receiveArr = UserSection::getCol([['id', 'in', $receiveIds]], 'name');
                    $info['receive_name'] = implode(',', $receiveArr);
                    $info['receive_ids']  = substr($info['receive_ids'], 0, strlen($info['receive_ids']) - 1);
                    break;
                case 3:
                    $receiveIds = explode(',', $info['receive_ids']);
                    $receiveArr = User::getCol([['id', 'in', $receiveIds]], 'name');
                    $info['receive_name'] = implode(',', $receiveArr);
                    break;
                default:
                    $info['receive_name'] = '';
                    break;
            }

            $info['send_name']   = User::getVal(['id' => $info['user_id']], 'name');
            $info['send_type']   = $sendType[$info['send_type']];
            $info['sign_nums']   = NoticeSign::getNum(['notice_id' => $info['id']]);
            $info['sign_status'] = NoticeSign::getFind(['notice_id' => $info['id'], 'user_id' => $this->user_id]) ? 1 : 0;

            unset($info['user_id']);
            unset($info['send_type']);
            unset($info['receive_ids']);
        }

        return Json::success('ok', $list, Notice::getNum($where));
    }

    /**
     * 通知公告发布
     */
    public function addNotice()
    {
        $param = input('');

        try{
            validate(NoticeValidate::class)->scene('add')->check($param);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        if(($param['send_type'] == 2 || $param['send_type'] == 3) && empty($param['receive_ids'])) return Json::fail('接收人不能为空');

        $param['user_id']     = $this->user_id;
        $param['receive_ids'] = $param['receive_ids'] . ',';

        $flag = Notice::add($param);
        if(!$flag) return Json::fail('发布失败');

        if($param['send_type'] == 1){
            $userIds = User::getCol(['is_staff' => 1], 'id');
        }else if($param['send_type'] == 2){
            $sectionIds = explode(',', $param['receive_ids']);
            array_pop($sectionIds);

            $userIds = User::getCol([['section_id', 'in', $sectionIds]], 'id');
        }

        SystemMenu::setRed($userIds, $this->user_id, 43, 1, $flag['id']);
        Message::setMessage($param['receive_ids'] ? $param['receive_ids'] : 0, $flag['id']);

        return Json::success('发布成功');
    }

    /**
     * 通知公告删除
     */
    public function delNotice()
    {
        $noticeId = Request()->param('notice_id', 0);
        if($noticeId == 0) return Json::fail('数据错误');

        $info = Notice::getFind(['id' => $noticeId]);

        if($info['send_type'] == 1){
            $userIds = User::getCol(['is_staff' => 1], 'id');
        }else if($info['send_type'] == 2){
            $sectionIds = explode(',', $info['receive_ids']);
            array_pop($sectionIds);

            $userIds = User::getCol([['section_id', 'in', $sectionIds]], 'id');
        }

        $userIds = 0;
        $flag = Notice::destroy($noticeId);
        if(!$flag) return Json::fail('删除失败');

        SystemMenu::delRed($userIds, 43, 1, $noticeId);
        
        return Json::success('删除成功');
    }

    /**
     * 通知公告签到
     */
    public function addNoticeSign()
    {
        $noticeId = Request()->param('notice_id', 0);
        if($noticeId == 0) return Json::fail('数据错误');

        $param['user_id']   = $this->user_id;
        $param['notice_id'] = $noticeId;

        $info = NoticeSign::getFind($param);
        if($info) return Json::fail('请勿重复签到');

        $flag = NoticeSign::add($param);
        if(!$flag) return Json::fail('签到失败');

        SystemMenu::delRed($this->user_id, 43, 1, 1, $noticeId);

        return Json::success('签到成功');
    }

    /**
     * 获取通知公告签到列表
     */
    public function getNoticeSignList()
    {
        $page     = Request()->param('page', 1);
        $noticeId = Request()->param('notice_id', 0);

        $info = Notice::getFind(['id' => $noticeId]);

        $userWhere[] = ['is_staff', '=', 1];

        switch ($info['send_type']) {
            case 2:
                $userIds = explode(',', $info['receive_ids']);
                array_pop($userIds);

                $userWhere[] = ['section_id', 'in', $userIds];
                break;
            case 3:
                $userIds = explode(',', $info['receive_ids']);
                array_pop($userIds);

                $userWhere[] = ['id', 'in', $userIds];
                break;
        }

        $list = User::getList($userWhere, 0, 0, 0, 'id, name, section_id, staff_no', 'id');

        $backData = [];
        foreach ($list as $key => $userInfo) {
            $signInfo = NoticeSign::getFind(['user_id' => $userInfo['id'], 'notice_id' => $noticeId]);

            $backInfo = [
                'name'         => $userInfo['name'],
                'staff_no'     => $userInfo['staff_no'],
                'section_name' => UserSection::getVal(['id' => $userInfo['section_id']], 'name'),
                'is_sign'      => $signInfo ? true : false,
                'sign_time'    => $signInfo ? $signInfo['create_time'] : '',
            ];

            $backData[] = $backInfo;
        }

        return Json::success('ok', $backData, User::getNum($userWhere));
    }




    /**
     * 获取工资类型
     */
    public function getSalaryType()
    {
        return Json::success('ok', UserSalaryType::getList([], 0));
    }




    /**
     * 获取部门级别列表
     */
    public function getSectionLevelList()
    {
        $page       = Request()->param('page', 1);
        $sectionId  = Request()->param('section_id', 0);
        $isAll      = Request()->param('is_all', 0);

        $field = 'id, name, level';

        $where[] = ['section_id', '=', $sectionId];

        if($isAll){
            $list = UserSectionLevel::getList($where, 0, 0, 0, $field);
        }else{
            $list = UserSectionLevel::getList($where, 1, $page, 20, $field, 'id');
        }
        
        if($list === false) return Json::fail('暂无数据');
        return Json::success('ok', $list, UserSectionLevel::getNum($where));
    }

    /**
     * 部门级别新增
     */
    public function addSectionLevel()
    {
        $id = Request()->param('id', 0);

        $param['name']       = Request()->param('name', '');
        $param['level']      = Request()->param('level', '');
        $param['section_id'] = Request()->param('section_id', '');

        if(empty($param['name'])) return Json::fail('名称不能为空');
        if(empty($param['level'])) return Json::fail('级别不能为空');
        if(empty($param['section_id'])) return Json::fail('部门不能为空');

        if($id){
            $isBe = UserSectionLevel::getFind([['level', '=', $param['level']], ['id', '<>', $id], ['section_id', '=', $param['section_id']]]);
            if($isBe) return Json::fail('操作失败，该级别已存在');

            $param['id'] = $id;
            $flag = UserSectionLevel::edit($param);
        }else{
            $isBe = UserSectionLevel::getFind(['level' => $param['level'], 'section_id' => $param['section_id']]);
            if($isBe) return Json::fail('操作失败，该级别已存在');

            $flag = UserSectionLevel::add($param);
        }
        
        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 部门级别删除
     */
    public function delSectionLevel()
    {
        $sectionLevelId = Request()->param('section_Level_id', 0);
        if($sectionLevelId == 0) return Json::fail('数据错误');

        $info = UserSectionLevel::getFind(['id' => $sectionLevelId]);

        $isDel = UserSectionLevel::getNum(['section_id' => $info['section']]);
        if($isDel) return Json::fail('该部门仅剩一个级别，不能删除');

        $flag = UserSectionLevel::destroy($sectionLevelId);
        if(!$flag) return Json::fail();
        return Json::success();
    }





    /**
     * 获取员工部门列表
     */
    public function getSectionList()
    {
        $page       = Request()->param('page', 1);
        $isAll      = Request()->param('is_all', 0);
        $cateId     = Request()->param('cate_id', 0);
        $isSign     = Request()->param('is_sign', 0);
        $signUserId = Request()->param('sign_user_id', -1);

        $where = [];
        $field = 'id, company_id, person_id, name, serial_no, people_num, is_out, brokerage_radio, salary_type_id, cate_id, attendance, meal, traffic, telephone';

        if($cateId != 0) $where[] = ['cate_id', '=', $cateId];

        if($isAll){
            if($isSign != 0){
                if($signUserId == 0){
                    $isUserRole = User::getUserRole($this->user_id);

                    if($isUserRole == 1){
                        $signUserIds = User::getCol(['section_id' => 6], 'id');
                        $signWhere = [['sign_user_id', 'in', $signUserIds]];
                    }else{
                        $signWhere = ['sign_user_id' => $this->user_id];
                    }
                }else if($signUserId > 0){
                    $signWhere = ['sign_user_id' => $signUserId];
                }else{
                    $signWhere = ['sign_user_id' => $this->user_id];
                }

                $companyIds = UserCompany::getCol($signWhere, 'id');
                if(!$companyIds) return Json::success('ok');
                $where[] = ['company_id', 'in', $companyIds];
            }
            $list = UserSection::getList($where, 0, 0, 0, $field);
        }else{
            $list = UserSection::getList($where, 1, $page, 20, $field, 'create_time');
        }
        
        if($list === false) return Json::fail('暂无数据');

        foreach($list as &$info){
            $info['salary_type_name'] = implode('、', UserSalaryType::getCol([['id', 'in', explode(',', $info['salary_type_id'])]], 'name'));
            $info['company_name']     = UserCompany::getVal(['id' => $info['company_id']], 'name');
            $info['person_name']      = $info['person_id'] ? User::getVal(['id' => $info['person_id']], 'name') : '';
        }

        return Json::success('ok', $list, UserSection::getNum($where));
    }

    /**
     * 员工部门新增
     */
    public function addSection()
    {
        $param = input('');

        try{
            validate(SectionValidate::class)->scene('add')->check($param);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $info = UserSection::getFind(['serial_no' => $param['serial_no']]);
        if($info) return Json::fail('编号已存在');

        $flag = UserSection::add($param);
        if(!$flag) return Json::fail('新增失败');

        CheckApprove::add(['section_id' => $flag['id']]);
        return Json::success('新增成功');
    }

    /**
     * 员工部门编辑
     */
    public function setSection()
    {
        $param = input('');

        try{
            validate(SectionValidate::class)->scene('edit')->check($param);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $param['id'] = $param['section_id'];

        $flag = UserSection::edit($param);
        if(!$flag) return Json::fail('编辑失败');
        return Json::success('编辑成功');
    }

    /**
     * 员工部门删除
     */
    public function delSection()
    {
        $sectionId = Request()->param('section_id', 0);
        if($sectionId == 0) return Json::fail('数据错误');

        $isDel = User::getFind(['section_id' => $sectionId, 'status' => 1]);
        if($isDel) return Json::fail('该部门下有员工，不能删除');

        $flag = UserSection::destroy($sectionId);
        if(!$flag) return Json::fail('删除失败');
        return Json::success('删除成功');
    }

    /**
     * 获取员工部门分类列表
     */
    public function getSectionCateList()
    {
        $list = UserSectionCate::getList([], 0);
        if($list === false) return Json::fail('暂无数据');
        return Json::success('ok', $list);
    }

    /**
     * 部门负责人列表
     */
    public function getPersonUserList()
    {
        return Json::success('ok', User::getList(['section_id' => 2], 0, 0, 0, 'id, name'));
    }



    /**
     * 获取角色列表
     */
    public function getRoleList()
    {
        $page  = Request()->param('page', 1);
        $isAll = Request()->param('is_all', 0);

        $field = 'id, name, rules';

        if($isAll){
            $list = SystemRole::getList([], 0, 0, 0, 'id, name');
        }else{
            $list = SystemRole::getList([], 1, $page, 20, $field);
        }
        
        if($list === false) return Json::fail('暂无数据');

        if($isAll == 0){
            foreach($list as &$info){
                $info['rules_name'] = implode('、', SystemMenu::getCol([['id', 'in', explode(',', $info['rules'])]], 'name'));
            }
        }

        return Json::success('ok', $list, SystemRole::getNum([]));
    }

    /**
     * 角色新增
     */
    public function addRole()
    {
        $param['name']  = Request()->param('name', '');
        $param['rules'] = Request()->param('rules', '');

        if(empty($param['name']) || empty($param['rules'])) return Json::fail('数据错误');

        $flag = SystemRole::add($param);
        if(!$flag) return Json::fail('新增失败');
        return Json::success('新增成功');
    }

    /**
     * 角色编辑
     */
    public function setRole()
    {
        $param['id']    = Request()->param('id', 0);
        $param['name']  = Request()->param('name', '');
        $param['rules'] = Request()->param('rules', '');

        if($param['id'] == 0 || empty($param['name']) || empty($param['rules'])) return Json::fail('数据错误');

        $flag = SystemRole::edit($param);
        if(!$flag) return Json::fail('编辑失败');
        return Json::success('编辑成功');
    }

    /**
     * 角色删除
     */
    public function delRole()
    {
        $RoleId = Request()->param('role_id', 0);
        if($RoleId == 0) return Json::fail('数据错误');

        $isDel = User::getFind(['role_id' => $RoleId]);
        if($isDel) return Json::fail('该身份下有员工，不能删除');

        $flag = SystemRole::destroy($RoleId);
        if(!$flag) return Json::fail('删除失败');
        return Json::success('删除成功');
    }



    /**
     * 获取权限列表
     */
    public function getMenuList()
    {
        $page  = Request()->param('page', 1);

        $field = 'id, pid, name, icon, redirect, sort, is_menu, is_show';
        $list = SystemMenu::getList([], 1, $page, 20, $field, 'sort DESC,id');
        
        if($list === false) return Json::fail('暂无数据');

        foreach($list as &$info){
            $pName = SystemMenu::getVal(['id' => $info['pid']], 'name');
            $info['p_name'] = $pName ? $pName : '';
        }

        return Json::success('ok', $list, SystemMenu::getNum([]));
    }

    /**
     * 获取二级权限树列表
     */
    public function getMenuTrees()
    {
        $field = 'id, pid, name';
        $list = SystemMenu::getList(['is_menu' => 1, 'pid' => 0], 0, 0, 0, $field, 'sort DESC');
        if($list === false) return Json::fail('暂无数据');

        foreach($list as &$info){
            $children = SystemMenu::getList(['is_menu' => 1, 'pid' => $info['id']], 0, 0, 0, $field, 'sort DESC');
            $info['children'] = $children ? $children : [];
        }

        return Json::success('ok', $list);
    }

    /**
     * 获取权限树列表
     */
    public function getMenuTree()
    {
        $field = 'id, pid, name';
        $list = SystemMenu::getList(['is_menu' => 1, 'pid' => 0], 0, 0, 0, $field, 'sort DESC');
        if($list === false) return Json::fail('暂无数据');

        foreach($list as &$info){
            $children = SystemMenu::getList(['is_menu' => 1, 'pid' => $info['id']], 0, 0, 0, $field, 'sort DESC');

            if($children){
                foreach($children as &$show){
                    $btnList = SystemMenu::getList(['is_menu' => 0, 'pid' => $show['id']], 0, 0, 0, 'id, pid, name');
                    $show['children'] = $btnList ? $btnList : [];
                }

                $info['children'] = $children;
            }else{
                $info['children'] = [];
            }
        }

        return Json::success('ok', $list);
    }

    /**
     * 权限新增
     */
    public function addMenu()
    {
        $param = input('');

        if(!empty($param['folder_name']) && !empty($param['file_name'])) $param['redirect'] = $param['folder_name'] . '/' . $param['file_name'];

        $flag = SystemMenu::add($param);
        if(!$flag) return Json::fail('新增失败');
        return Json::success('新增成功');
    }

    /**
     * 权限编辑
     */
    public function setMenu()
    {
        $param = input('');

        if(!empty($param['folder_name']) && !empty($param['file_name'])) $param['redirect'] = $param['folder_name'] . '/' . $param['file_name'];

        $flag = SystemMenu::edit($param);
        if(!$flag) return Json::fail('编辑失败');
        return Json::success('编辑成功');
    }

    /**
     * 权限删除
     */
    public function delMenu()
    {
        $menuId = Request()->param('menu_id', 0);
        if($menuId == 0) return Json::fail('数据错误');

        $flag = SystemMenu::destroy($menuId);
        if(!$flag) return Json::fail('删除失败');
        return Json::success('删除成功');
    }



    /**
     * 考勤审批列表
     */
    public function getApproveList()
    {
        $page = Request()->param('page', 1);

        $list = CheckApprove::getList([], 1, $page, 20, '*', 'id DESC');
        if(!$list) return Json::fail('暂无数据');

        foreach($list as &$info){
            $info['user_name']    = User::getVal(['id' => $info['user_id']], 'name');
            $info['section_name'] = UserSection::getVal(['id' => $info['section_id']], 'name');
            $info['approve_name'] = User::getVal(['id' => $info['approve_id']], 'name');
            $info['send_name']    = User::getVal(['id' => $info['send_id']], 'name');
        }

        return Json::success('ok', $list, CheckApprove::getNum([]));
    }

    /**
     * 考勤审批编辑
     */
    public function setApprove()
    {
        $param['id']         = Request()->param('id', 1);
        $param['section_id'] = Request()->param('section_id', 0);
        $param['approve_id'] = Request()->param('approve_id', 1);
        $param['send_id']    = Request()->param('send_id', 1);

        $flag = CheckApprove::edit($param);
        if(!$flag) return Json::fail();
        return Json::success();
    }



    /**
     * 意见反馈
     */
    public function getFeedbackList()
    {
        $page = Request()->param('page', 1);

        $field = 'id, content, img, create_time';
        $list = Feedback::getList([], 1, $page, 20, $field, 'create_time DESC');
        if(!$list) return Json::fail('暂无数据');
        return Json::success('ok', $list, Feedback::getNum([]));
    }

    /**
     * 弹幕通知列表
     */
    public function getMessage()
    {
        $list = Message::where('is_read=0 and (user_id=0 or FIND_IN_SET(' . $this->user_id . ', user_id))')
            ->field('id, type, title, content')
            ->order('create_time DESC')
            ->select();

        if($list->isEmpty()) return Json::fail('暂无数据');
        return Json::success('ok', $list);
    }

    /**
     * 弹幕已读
     */
    public function setNews()
    {
        $flag = Message::edit(['id' => Request()->param('id', 0), 'is_read' => 1]);
        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 公司列表
     */
    public function getCompanyList()
    {
        $page  = Request()->param('page', 1);
        $isAll = Request()->param('is_all', 0);

        if($isAll){
            $list = UserCompany::getList([], 0, 0, 0, 'id, name');
        }else{
            $list = UserCompany::getList([], 1, $page, 20, '*', 'id');
        }

        if(!$isAll){
            foreach($list as &$info){
                $info['sign_user_name']   = User::getVal(['id' => $info['sign_user_id']], 'name');
            }
        }

        return Json::success('ok', $list, UserCompany::getNum([]));
    }

    /**
     * 公司新增&编辑
     */
    public function setCompany()
    {
        $id= Request()->param('id', 0);

        $param['name']         = Request()->param('name', '');
        $param['sign_user_id'] = Request()->param('sign_user_id', 0);
        if(empty($param['name'])) return Json::fail('名称不能为空');

        if($id){
            $param['id'] = $id;
            $flag = UserCompany::edit($param);
        }else{
            $flag = UserCompany::add($param);
        }

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 公司删除
     */
    public function delCompany()
    {
        $companyId = Request()->param('company_id', 0);
        if($companyId == 0) return Json::fail('数据错误');

        $flag = UserCompany::destroy($companyId);
        if(!$flag) return Json::fail();
        return Json::success();
    }




    /**
     * 打卡图片列表
     */
    public function getCheckImgList()
    {
        $page = Request()->param('page', 1);

        $list = CheckImg::getList([], 1, $page, 20, '*', 'id DESC');
        if(!$list) return Json::fail('暂无数据');

        foreach ($list as &$info) {
            $info['image'] = getUrl() . $info['image'];
        }

        return Json::success('ok', $list);
    }

    /**
     * 打卡图片新增&编辑
     */
    public function setCheckImg()
    {
        $id      = Request()->param('id', 0);
        $image   = Request()->file('image', '');
        $content = Request()->param('content', '');

        if($id){
            if(empty($image) && empty($content)) return Json::fail();

            $param['id'] = $id;
            if(!empty($image)) $param['image'] = fileUp($image, 'check_img');
            if(!empty($content)) $param['content'] = $content;

            $flag = CheckImg::edit($param);
        }else{
            $param['image']   = fileUp($image, 'check_img');
            $param['content'] = $content;

            $flag = CheckImg::add($param);
        }

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 打卡图片删除
     */
    public function delCheckImg()
    {
        $flag = CheckImg::destroy(Request()->param('id', 0));
        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 意见反馈
     */
    public function setFeedback()
    {
        $content = Request()->param('content', '');
        $img     = Request()->param('img', '');
        $imgArr = json_decode($img, true);
        if(empty($content) && !$img) return Json::fail('内容或图片不能为空');

        if(count($imgArr) > 5) return Json::fail('图片最多上传5张');

        $flag = Feedback::add(['user_id' => $this->user_id, 'content' => $content, 'img' => $img]);
        if(!$flag) return Json::fail();
        return Json::success();
    }
}